package thread_pool;


/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/23
 * Time:16:40
 * 不可继承性-解决方案
 **/
public class PoolDemo31 {

    static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();


    public static void main(String[] args) throws InterruptedException {
        //set threadLocal
        //threadLocal.set("java");

        /*Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                //get threadLocal
                String result = threadLocal.get();
                System.out.println("结果"+result);
            }
        });
        t1.start();*/

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                // set ThreadLocal
                threadLocal.set("Java");
                System.out.println("线程 1 设置了值：Java");
            }
        });
        t1.start();

        // 等待执行完
        t1.join();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                // get ThreadLocal
                String result = threadLocal.get();
                System.out.println("线程 2 得到结果：" + result);
            }
        });
        t2.start();

    }
}
